
// # dev开发环境、prod 生产环境
// const BASE_URL_DEV = 'https://zyydtest.zhongyanyude.cn/devapi'; // dev开发环境
// const BASE_URL_PROD = 'https://zyyd.zhongyanyude.cn/prodapi'; // prod 生产环境
import { BASE_URL_API } from "../common/config.js";
import { NEW_URL_DEV } from "@/common/config.js"; 
const BASE_URL = BASE_URL_API;

// 封装的请求函数
export const request = (options) => {
	//获取token
	// const token = uni.getStorageSync('token');

  return new Promise((resolve, reject) => {
    //封装主体：网络请求
	uni.request({
      url: BASE_URL + options.url,
	  //默认值GET，如果有需要改动，在options中设定其他的method值
      method: options.method || 'GET',
      data: options.data || {},	  
      header: options.header || {
        'Content-Type': 'application/json'
      },
	  timeout: 10000,
	  
      success: (res) => {
		console.log("request:",res);
        if (res.statusCode === 200) {
			if(res.data.code === 200) {
				resolve(res.data);
			} else {
				handleCodeError(res);	
				// reject(res);							
			}
          
        } else {
          handleError(res);
        }
      },
      fail: (err) => {
        uni.showToast({
          title: '请求失败，请检查网络',
          icon: 'none'
        });
        reject(err);
      }

    });
  });
};
//内部逻辑验证错误处理
const handleCodeError = (res) => {
  switch (res.data.code) {
    case 401:
	  uni.showModal({
	  	title: '提示',
	  	content: '未授权(401)',
		showCancel: false,
	  	success: function (res) {
	  		if (res.confirm) {
	  			uni.clearStorageSync();
	  			uni.navigateTo({
	  			  url: '/pages/login/login'
	  			})
	  		} 
	  	}
	  });
      break;
    default:
      uni.showToast({
        title: `${res.data.msg}`,
        icon: 'none'
      });	  
  }
};

// 错误处理函数
const handleError = (res) => {
  switch (res.statusCode) {
    case 400:
      uni.showToast({
        title: '请求错误(400)',
        icon: 'none'
      });
      break;
    case 401:
	  uni.showModal({
	  	title: '提示',
	  	content: '未授权(401)',
		showCancel: false,
	  	success: function (res) {
	  		if (res.confirm) {
	  			uni.clearStorageSync();
	  			uni.navigateTo({
	  			  url: '/pages/login/login'
	  			})
	  		} 
	  	}
	  });
      break;
    case 404:
      uni.showToast({
        title: '资源未找到(404)',
        icon: 'none'
      });
      break;
    case 500:
      uni.showToast({
        title: '服务器错误(500)',
        icon: 'none'
      });
      break;
    default:
      uni.showToast({
        title: `请求失败(${res.statusCode})`,
        icon: 'none'
      });
  }
};
// GET请求
export const get = (url, data, header) => {
  return request({
    url,
    method: 'GET',
    data,
	header
  });
};

// GET请求带鉴权
export const getInfo = (url) => {
  //获取token
  const token = uni.getStorageSync('login_token');	
  return request({
    url,
    method: 'GET',
	header:{
		'Content-Type': 'application/json',
		'Authorization': `Bearer `+ token, // 将 token 添加到请求头中
	}
  });
};
get
// POST请求
export const post = (url, data, header) => {

	//获取token
	const token = uni.getStorageSync('login_token');
	
	return request({
	url,
	method: 'POST',
	data,
	header:{
		'Content-Type': 'application/json',
		'Authorization': `Bearer `+token, // 将 token 添加到请求头中
	}
	});
};
// DELETE 请求
export const del = (url, data, header) => {
	//获取token
	const token = uni.getStorageSync('login_token');
	console.log('request-del-token:'+token);
	
	url = url+'/'+data.id;
	
  return request({
    url,
    method: 'DELETE',
    // data,
    header:{
    	// 'Content-Type': 'application/x-www-form-urlencoded',
    	'Authorization': `Bearer `+token, // 将 token 添加到请求头中
    }
  });
};

// 文件上传
export const uploadFile = (url, filePath, name) => {
	//获取token
	const token = uni.getStorageSync('login_token');
	
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: BASE_URL + url,
			filePath : filePath,
			name : 'file',
			header:{
				'Content-Type': 'application/json',
				// 'Accept': 'application/json;charset=utf-8',
				'Authorization': `Bearer `+token, // 将 token 添加到请求头中
			},
			success: (res) => {
			if (res.statusCode === 200) {
					resolve(JSON.parse(res.data));
				} else {
					handleError(res);
					reject(res);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};
